package com.renli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renli.exception.RenLiException;
import com.renli.mapper.StaffWagesMapper;
import com.renli.model.domain.StaffWages;
import com.renli.model.domain.WorkDay;
import com.renli.result.R;
import com.renli.service.StaffWagesService;
import com.renli.service.WorkDayService;
import com.renli.utils.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StaffWagesServiceImpl extends ServiceImpl<StaffWagesMapper, StaffWages> implements StaffWagesService {


    @Autowired
    private WorkDayService workDayService;


    /**
     * 员工信息分页查询
     * @return
     */
    @Override
    public R pageAll(int page, int size, Long id, String name, Double salary) {
        if (page==0||size==0){
            throw new RenLiException("前端大哥您好，您传递的page或者size是不是有一个或多个为0");
        }
        Page<StaffWages> page1=new Page<>(page,size);
        LambdaQueryWrapper<StaffWages> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,StaffWages::getName,name);
        queryWrapper.orderByDesc(StaffWages::getThisMonthsSalary);
        queryWrapper.eq(id!=null,StaffWages::getStaffId,id);
        queryWrapper.ge(salary!=null,StaffWages::getThisMonthsSalary,salary);
        return new R(1,this.page(page1,queryWrapper));
    }

    /**
     * 每月1号更新表
     * @return
     */
    @Override
    public R updateMonthOne() {
        if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH)==1){
             //首先更新workday表
            updateWorkday();
            //更新staffwages表
            updateStaffWages();
        }
        return new R(1,"新的一个月表更新成功");
    }

    private void updateStaffWages() {
        List<StaffWages> staffWages = this.list();
        staffWages.stream().peek((item)->{
            item.setFullAttendenceAward(300.0);
            item.setOtherPay((double) 0);
            item.setLatePayAll((double) 0);
            item.setPostPayAll(item.getPostPay()* Date.getCurrentMonthLastDay());
            item.setDateLateAll((double) 0);
            item.setOvertimePayAll((double) 0);
            Double thisMonthsSalary = item.getThisMonthsSalary();
            item.setLastMonthsSalary(thisMonthsSalary);
            Double all=item.getPostPay()* Date.getCurrentMonthLastDay()+300+item.getBasicWage();
            item.setThisMonthsSalary(all);
        }).collect(Collectors.toList());
    }

    private void updateWorkday() {
        List<WorkDay> workDays = workDayService.list();
        workDays.stream().peek((item)->{
            item.setFullAttendance(1);
            item.setDayLate(0);
            item.setDaysLost(0);
            item.setWorkOvertime(0);
            workDayService.updateById(item);
        }).collect(Collectors.toList());
    }
}
